/*******************************************************************************
 ** Name: usart.h                                                             **
 ** Description: ATmega328p (or generic) USART routines                       **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the LICENSE file within             **
 ** this distribution or at www.opendesignstrategies.org/license.             **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $HOME_DIR/LICENSE.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 ** Copyright (c) 2009-2012 Open Design Strategies, LLC                       **
 ** All rights reserved.                                                      **
 ******************************************************************************/
// $HeadURL$
// Modified $Date$ by $Author$
// $Id$

#ifndef USART_H_
#define USART_H_

#include "common.h"

// defaults set for our arduino/atmega328p type board for convenience
#define FOSC 16000000UL
#define BAUD 57600
#define UBRR (FOSC/16*BAUD)-1UL

class USART
{
public:

	void	Init() {
		UCSR0B = (1<<RXEN0)|(1<<TXEN0);
		UBRR0L = (UBRR & 0xFF);
		UBRR0H = (UBRR >> 8);
	}
	void	SendByte(unsigned char b) { tx(b); }
	char	RecvByte() { return rx(); }
	void	WriteLine(const char * msg, bool bPgmSpace = true) {
		if (bPgmSpace)
		{
			while (pgm_read_byte(msg) != 0x00) {
				while (!(UCSR0A & (1<<UDRE0)));
				UDR0 = pgm_read_byte(msg++);
			}
		}
		else
		{
			while (*msg != 0x00) {
				while (!(UCSR0A & (1<<UDRE0)));
				UDR0 = *(msg++);
			}
		}
	}
	void	EnableInterrupt(uint8_t dir) {
		if (d == USART_TX) UCSR0B |= (1<<RXCIE0);
		if (d == USART_RX) UCSR0B |= (1<<TXCIE0);
	}
	void	tx(unsigned char c) {
		while(!(UCSR0A & (1<<UDRE0)));
		UDR0 = c;
	}
	char	rx() {
		while (!(UCSR0A & (1<<RXC0)));
		return UDR0;
	}
	
private:
	
};


#endif /* USART_H_ */